<?php
/**
 * @brief attack_matching protocol
 *
 * @param post uid User ID
 * @return Matching User IDs
 * @author con100 <con100@playplus.net>
 * @since 11/04/2013
 * @copyright Copyright (c) 2013, PlayPlus
 **/

include 'dbconn.php';
include 'include.php';
include 'config.php';

$user_id = mysql_real_escape_string($_POST['uid']);

$enemy_infos = array();
$time = time();

$sql = "select id, level, name, hp, timestemp_hp from ebs_member where shield = 'N' and ($time - timestemp_active) > 300 order by level desc";
$res = mysql_query($sql);

while($data = mysql_fetch_assoc($res)){
	$enemy_infos[] = $data;
}
if($enemy_infos == null){
	$result = array("Result"=>FALSE);
}
else{
	$result = array("Result"=>TRUE, "enemy_info"=>$enemy_infos);
}
echo json_encode($result);
/*
header('Content-type: text/xml; charset=UTF-8');

$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(true);
$writer->setIndentString('  ');
$writer->startDocument('1.0', 'UTF-8');

$writer->startElement('attack_matching');
	$writer->startElement('result');
	$writer->text($result);
	$writer->endElement();
	if($result == "TRUE"){
		$writer->startElement('enemy_info');
		for($i=0;$i<count($enemy_infos);$i++){
			$writer->startElement('enemy');
				$writer->writeAttribute('id', $enemy_infos[$i]['id']);
				$writer->writeAttribute('level', $enemy_infos[$i]['level']);
				$writer->writeAttribute('name', $enemy_infos[$i]['name']);
				$writer->writeAttribute('hp', $enemy_infos[$i]['hp']);
				$writer->writeAttribute('timestemp_hp', $enemy_infos[$i]['timestemp_hp']);
			$writer->endElement();
		}
		$writer->endElement();
	}
$writer->endElement();

echo($writer->outputMemory());

/*
$sql = "select hp, timestemp_hp, max_hp  from ebs_member where id='$user_id'";
$res = mysql_query($sql);
$data = mysql_fetch_array($res);

$time_hp = (int)($data['hp']+(time()-$data['timestemp_hp']) * $hp_sec);
if($data['hp'] > 0){
	$data['hp'] = min($time_hp, $data['max_hp']);
}else if($time_hp > $data['max_hp']){
	$data['hp'] = $data['max_hp'];
}

if($no_nation != 0 || !$fold){
	$sql = "select * from ebs_member where nation='$no_nation' order by level desc";
	$res = mysql_query($sql);
	while($row = mysql_fetch_assoc($res)){
		//hp 보정
		$time_hp = (int)($row['hp']+(time()-$row['timestemp_hp']) * $hp_sec);
		if($row['hp'] > 0){
			$row['hp'] = min($time_hp, $row['max_hp']);
		}else if($time_hp > $row['max_hp']){
			$row['hp'] = $row['max_hp'];
		}
		//접속상태
		if(time()-$row['timestemp_active'] < 5*60){
			$online = 1;
		}else{
			$online = 0;
		}
		//장착 아이템
		$inven = null;
		$sql = "select type,value from ebs_inven where no='$row[no]' and equip=1";
		$res_inv = mysql_query($sql);
		while($row_inv = mysql_fetch_assoc($res_inv)){
			if(!$inven[$row_inv[type]]){
				$inven[$row_inv[type]] = $row_inv[value];
			}
		}
		
		if($row[hp] > 0){
		}
	}
}else{
	$level_low = max($data[level]*0.5, 0);
	$level_high = max($data[level]+2, 0);
	$sql = "select * from ebs_member where nation='$no_nation' and level>='$level_low' and level<='$level_high' order by level desc";
	$res = mysql_query($sql);
	$num_rows = mysql_num_rows($res);
	$count = 0;
	while($row = mysql_fetch_assoc($res)){
		if($count < 10){
			//hp 보정
			$time_hp = (int)($row['hp']+(time()-$row['timestemp_hp']) * $hp_sec);
			if($row['hp'] > 0){
				$row['hp'] = min($time_hp, $row['max_hp']);
			}else if($time_hp > $row['max_hp']){
				$row['hp'] = $row['max_hp'];
			}
			//접속상태
			if(time()-$row['timestemp_active'] < 5*60){
				$online = 1;
			}else{
				$online = 0;
			}
			//장착 아이템
			$inven = null;
			$sql = "select type,value from ebs_inven where no='$row[no]' and equip=1";
			$res_inv = mysql_query($sql);
			while($row_inv = mysql_fetch_assoc($res_inv)){
				if(!$inven[$row_inv[type]]){
					$inven[$row_inv[type]] = $row_inv[value];
				}
			}
			
			if($row[hp] > 0){
				$count++;
			}
		}
	}
}
*/
?>